package com.wl.emp.servlet;

import com.alibaba.fastjson.JSONObject;
import com.wl.emp.common.ResultData;
import com.wl.emp.entity.Emp;
import com.wl.emp.service.EmpService;
import com.wl.emp.service.impl.EmpServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@WebServlet("/emp/*")
public class EmpServlet extends HttpServlet {

    private EmpService empService = new EmpServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String requestURI = req.getRequestURI();
        String methodName = requestURI.substring(requestURI.lastIndexOf("/") + 1);

        switch (methodName){
            case "insert": insert(req,resp);break;
            case "update": update(req,resp);break;
            case "delete": delete(req,resp);break;
            case "selectById": selectById(req,resp);break;
            case "selectByName": selectByName(req,resp);break;
            case "selectByHiredate": selectByHiredate(req,resp);break;
            case "selectAll": selectAll(req,resp);break;
        }
    }



    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }

    public void insert(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 1.获取
        String empno = req.getParameter("empno");
        String ename = req.getParameter("ename");
        String job = req.getParameter("job");
        String mgr = req.getParameter("mgr");
        String hiredate = req.getParameter("hiredate");
        String sal = req.getParameter("sal");
        String comm = req.getParameter("comm");
        String deptno = req.getParameter("deptno");
        // 2.封装
        Emp emp = new Emp();
        if (empno != null) {
            emp.setEmpno(Integer.valueOf(empno));
        }
        emp.setEname(ename);
        emp.setJob(job);
        if (mgr != null){
            emp.setMgr(Integer.valueOf(mgr));
        }
        if (hiredate != null){
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            try {
                Date date = sdf.parse(hiredate);
                emp.setHiredate(date);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        if (sal != null){
            emp.setSal(Integer.valueOf(sal));
        }
        if (comm != null){
            emp.setComm(Integer.valueOf(comm));
        }
        if (deptno != null){
            emp.setDeptno(Integer.valueOf(deptno));
        }

        int row = empService.insertSelective(emp);

        PrintWriter out = resp.getWriter();
        ResultData result = new ResultData();

        if (row == 1){
            result.setCode(200);
            result.setMessage("添加成功");
        }else{
            result.setCode(10001);
            result.setMessage("添加失败");
        }

        out.write(JSONObject.toJSONString(result));
        out.flush();
    }
    public void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 1.获取
        String empno = req.getParameter("empno");
        String ename = req.getParameter("ename");
        String job = req.getParameter("job");
        String mgr = req.getParameter("mgr");
        String hiredate = req.getParameter("hiredate");
        String sal = req.getParameter("sal");
        String comm = req.getParameter("comm");
        String deptno = req.getParameter("deptno");
        // 2.封装
        Emp emp = new Emp();
        if (empno != null) {
            emp.setEmpno(Integer.valueOf(empno));
        }
        emp.setEname(ename);
        emp.setJob(job);
        if (mgr != null){
            emp.setMgr(Integer.valueOf(mgr));
        }
        if (hiredate != null){
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            try {
                Date date = sdf.parse(hiredate);
                emp.setHiredate(date);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        if (sal != null){
            emp.setSal(Integer.valueOf(sal));
        }
        if (comm != null){
            emp.setComm(Integer.valueOf(comm));
        }
        if (deptno != null){
            emp.setDeptno(Integer.valueOf(deptno));
        }

        int row = empService.updateByPrimaryKeySelective(emp);

        PrintWriter out = resp.getWriter();
        ResultData result = new ResultData();

        if (row == 1){
            result.setCode(200);
            result.setMessage("更新成功");
        }else{
            result.setCode(10001);
            result.setMessage("更新失败");
        }

        out.write(JSONObject.toJSONString(result));
        out.flush();
    }
    public void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 1.获取
        String empno = req.getParameter("empno");

        int row = empService.deleteByPrimaryKey(Integer.valueOf(empno));
        PrintWriter out = resp.getWriter();
        ResultData result = new ResultData();

        if (row == 1){
            result.setCode(200);
            result.setMessage("删除成功");
        }else{
            result.setCode(10001);
            result.setMessage("删除失败");
        }

        out.write(JSONObject.toJSONString(result));
        out.flush();
    }

    public void selectById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 1.获取
        String empno = req.getParameter("empno");
        Emp emp = empService.selectByPrimaryKey(Integer.valueOf(empno));

        PrintWriter out = resp.getWriter();
        ResultData result = new ResultData();

        if (emp != null){
            result.setCode(200);
            result.setMessage("查询成功");
            result.setData(emp);
        }else{
            result.setCode(10001);
            result.setMessage("查询失败");
        }

        out.write(JSONObject.toJSONString(result));
        out.flush();
    }
    public void selectByName(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 1.获取
        String ename = req.getParameter("ename");
        List<Emp> emps = empService.selectByEname(ename);

        PrintWriter out = resp.getWriter();
        ResultData result = new ResultData();

        if (emps != null && !emps.isEmpty()){
            result.setCode(200);
            result.setMessage("查询成功");
            result.setData(emps);
        }else{
            result.setCode(10001);
            result.setMessage("查询失败");
        }

        out.write(JSONObject.toJSONString(result));
        out.flush();
    }
    public void selectByHiredate(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 1.获取
        String start = req.getParameter("start");
        String end = req.getParameter("end");

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date s1 = sdf.parse(start);
            Date s2 = sdf.parse(end);
            List<Emp> emps = empService.selectByHiredate(s1,s2);


            PrintWriter out = resp.getWriter();
            ResultData result = new ResultData();

            if (emps != null && !emps.isEmpty()){
                result.setCode(200);
                result.setMessage("查询成功");
                result.setData(emps);
            }else{
                result.setCode(10001);
                result.setMessage("查询失败");
            }

            out.write(JSONObject.toJSONString(result));
            out.flush();

        } catch (ParseException e) {
            e.printStackTrace();
        }
    }

    public void selectAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<Emp> emps = empService.selectAll();

        PrintWriter out = resp.getWriter();
        ResultData result = new ResultData();

        if (emps != null && !emps.isEmpty()){
            result.setCode(200);
            result.setMessage("查询成功");
            result.setData(emps);
        }else{
            result.setCode(10001);
            result.setMessage("查询失败");
        }

        out.write(JSONObject.toJSONString(result));
        out.flush();

    }
}
